package com.supermap.business.cloud.push;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.adv.AdvisoryCloudService;
import com.supermap.dubbo.api.cloud.push.Notice;
import com.supermap.dubbo.api.cloud.push.PushService;

import com.supermap.dubbo.api.cloud.tracesource.TraceSourceService;
import com.supermap.dubbo.api.cloud.warnInfo.WarnInfo;
import com.supermap.dubbo.api.cloud.warnInfo.WarningInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author aqua
 * @description
 * @date 2018年11月22 下午 3:39
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/cloud/push")
public class PushController {
    /**
     * websochet消息发送对象
     */
    @Reference(timeout = 10000)
    private WarningInfoService warningInfoService;
    @Reference(timeout = 100000)
    private AdvisoryCloudService advisoryCloudService;
    @Reference(timeout = 100000)
    private PushService pushService;
    @Reference(timeout = 100000)
    private TraceSourceService traceSourceService;

    @GetMapping("/getNoticeList")
    @ResponseBody
    public JSONUtils getNoticeList(String noticetype, String userId, Integer pageSize){

        return  pushService.getNoticeList(noticetype,userId,pageSize);
    }

    @GetMapping("/getReceiveuserIdCount")
    @ResponseBody
    public JSONUtil getReceiveuserIdCount(String receiveuserid,String productId) {
        if(productId==null){
            //默认全国APP项目
            productId="quanguoapp2019131xsadwqeq";
        }
        //推送内部用户
        String noticetype=pushService.getProductNoticeTypes(productId);
        JSONUtil  jsonUtil=  pushService.getReceiveuserIdCount(receiveuserid,noticetype);
       return  jsonUtil;
    }

    @GetMapping("/getGroupReceiveuserIdCount")
    @ResponseBody
    public JSONUtil getGroupReceiveuserIdCount(String receiveuserid,String noticetype) {
        JSONUtil  jsonUtil=  pushService.getGroupReceiveuserIdCount(receiveuserid,noticetype);
        return  jsonUtil;
    }
    @GetMapping("/getUserRoleByuserId")
    @ResponseBody
    public Map<String,Object> getUserRoleByuserId(String userId) {
        return  advisoryCloudService.getUserRoleByuserId(userId);
    }

    @GetMapping("/getNoticetypeReceiveuserIdList")
    @ResponseBody
    public JSONUtil getNoticetypeReceiveuserIdList(String receiveuserid, String noticetype,Integer pageNum,Integer pageSize) {
        JSONUtil jsonUtil=new JSONUtil();
       List<Map<String,Object>> mapList= pushService.
               getNoticetypeReceiveuserIdList(receiveuserid,noticetype,pageNum,pageSize);
       if(mapList!=null&&mapList.size()>0){
            //角色识别
           for (Map<String,Object> map:mapList){
               String userId=(String)map.get("senduserid");
               Integer messageid=(Integer)map.get("messageid");
                if(userId!=null){
                    Map<String,Object> userRoleMap= advisoryCloudService.getUserRoleByuserId(userId);
                    if(userRoleMap!=null&&userRoleMap.size()>0){
                        map.put("roleCode",userRoleMap.get("roleCode"));
                        map.put("roleName",userRoleMap.get("roleName"));
                        map.put("showName",userRoleMap.get("showName"));
                    }
                    if(noticetype.equals("7")){
                     JSONUtil json=   warningInfoService.getWarnInfoById(messageid);
                        WarnInfo warnInfo=  (WarnInfo)json.getData();
                        map.put("meteoName",warnInfo.getSender());
                    }
                }else{
                    map.put("roleCode","");
                    map.put("roleName","");
                    map.put("showName","");
                }
           }

       }
        return jsonUtil.setData(mapList);
    }

    @PutMapping("/putNoticeRead")
    @ResponseBody
    public JSONUtil putNoticeRead(Integer noticeId,String receiveuserid,String productId) {
        if(productId==null){
            //默认全国APP项目
            productId="quanguoapp2019131xsadwqeq";
        }
        JSONUtil jsonUtil=  pushService.putNoticeRead(noticeId);
        //app总数推送
        if(productId!=null&&receiveuserid!=null){
            String noticetype=pushService.getProductNoticeTypes(productId);
            MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(receiveuserid,noticetype).getJsonStr(),receiveuserid);
        }
        return  jsonUtil;
    }

    @PutMapping("/putNoticeCode")
    @ResponseBody
    public JSONUtil putNoticeCode(String code,Integer userId) {
        return  pushService.putNoticeCode(code,userId);
    }

    /**
     * 根据区域手动推送
     * */
    @PutMapping("/putManualNoticeList")
    @ResponseBody
    public JSONUtil getAllUserIdByAreaCode(String areaCode,String dateTime,Integer notType,Integer messageid,String title,String sendUserId ) {
        List<String> integerList= pushService.getAllUserIdByAreaCode(areaCode);
        List<Notice> noticeList=new ArrayList<>();
        if(integerList!=null&&integerList.size()>0){
            for(String userId:integerList){
                Notice notice = new Notice();
                notice.setUploadtime(dateTime);
                notice.setNoticetype(notType);
                notice.setMessageid(messageid);
                notice.setNoticetitle(title);
                notice.setReceiveuserid(userId);
                notice.setSenduserid(sendUserId);
                noticeList.add(notice);
            }
        }
        return  new JSONUtil().setData(pushService.putNoticeList(noticeList));
    }








}
